<?php
class ShopPage extends Page {
    
    static $db = array(
        'Type' => 'Float',
        'ThalsOnly' => 'Boolean',
        'PremiumOnly' => 'Boolean',
        'DiscountOnly' => 'Boolean'
    );
    
    static $icon = 'themes/default/images/icons/shoppage';
    
    function getCMSFields() {
        $fields = parent::getCMSFields();   
        $fields->addFieldToTab('Root.Content.Main', new DropdownField('Type','Type Pagina', array('1' => 'A-Z', '2' => 'Gerangschikt op categorie', '3' => 'Gerangschikt op straat')));
        $fields->addFieldToTab('Root.Content.Main', new CheckboxField('ThalsOnly','Alleen winkels aangesloten bij Thals laten zien'));
        $fields->addFieldToTab('Root.Content.Main', new CheckboxField('PremiumOnly','Alleen PREMIUM winkels laten zien'));
        $fields->addFieldToTab('Root.Content.Main', new CheckboxField('DiscountOnly','Alleen winkels laten zien die kadocheques aanbieden'));
        $fields->removeFieldFromTab('Root.Content.Main', 'Content');
        $fields->removeFieldFromTab('Root.Content.Main', 'Image');
        return $fields;
    }
}
 
class ShopPage_Controller extends Page_Controller {
    
    static $allowed_actions = array(
        'details'
    );
    
    public function init() {
	parent::init();
    }
    
    protected static $shopLetters = array('0-9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
    
    // Verkrijgen van de letters die naar de winkels linken aan de top van de pagina
    public function getAZLetters() {
        $this->ThalsOnly ? $thalsfilter = "IsThals = '1'" : $thalsfilter = "1=1";
        $this->PremiumOnly ? $premiumfilter = " AND IsPremium = '1'" : $premiumfilter = "";
        $this->DiscountOnly ? $discountfilter = " AND IsDiscount = '1'" : $discountfilter = "";
        $filter = $thalsfilter.$premiumfilter.$discountfilter; 

        $sqlQuery = new SQLQuery();
        $sqlQuery->select = array("FirstLetter");
        $sqlQuery->from = array("Shop");
        $sqlQuery->where = array($filter);
        $sqlQuery->orderby = "FirstLetter ASC";
        $sqlQuery->groupby = array("FirstLetter");
        $result = $sqlQuery->execute();
        
        $results = array();
        $c = 0; 
        
        foreach($result as $row) {
            
             if (preg_match("/^[0-9]$/", $row['FirstLetter'])) {
                $row['FirstLetter'] = '0-9'; 
                if($c==0) {
                    $results[] = $row['FirstLetter'];
                }
                $c++;
            } else {
                $results[] = $row['FirstLetter'];
            }
                
        } 
        
        $dataObject = array();
        foreach(self::$shopLetters as $letter){
            $dataObject[] = array(
                'ShopLetter' => $letter,
                'ShopLink' => in_array($letter,$results) ? $letter : false
            );
            
        }
        return new DataObjectSet($dataObject); 
        
    }
    
    // Verkrijgen van de categorieen die naar de winkels linken aan de top van de pagina
    public function getCategories() {
        $this->ThalsOnly ? $thalsfilter = "IsThals = '1'" : $thalsfilter = "1=1";
        $this->PremiumOnly ? $premiumfilter = " AND IsPremium = '1'" : $premiumfilter = "";
        $this->DiscountOnly ? $discountfilter = " AND IsDiscount = '1'" : $discountfilter = "";
        $filter = $thalsfilter.$premiumfilter.$discountfilter;
                
        $sqlQuery = new SQLQuery();
        $sqlQuery->select = array("CategoryID", "Category.Name AS CatName");
        $sqlQuery->from = array("Shop_Categories", "LEFT JOIN Category ON Shop_Categories.CategoryID = Category.ID LEFT JOIN Shop ON Shop_Categories.ShopID = Shop.ID");
        $sqlQuery->where = array($filter);
        $sqlQuery->orderby = "CatName ASC";
        $sqlQuery->groupby = array("CategoryID");
        $result = $sqlQuery->execute();
        
        $results = array();
        $c = 0; 
        
        foreach($result as $row) {
            
             $results[] = array(
                'ID' => $row['CategoryID'], 
                'Name' => $row['CatName']
             ); 
                       
        } 
        
        return new DataObjectSet($results); 
        
    }
    
    // Verkrijgen van de straten die naar de winkels linken aan de top van de pagina
    public function getStreets() {
        $this->ThalsOnly ? $thalsfilter = "IsThals = '1'" : $thalsfilter = "1=1";
        $this->PremiumOnly ? $premiumfilter = " AND IsPremium = '1'" : $premiumfilter = "";
        $this->DiscountOnly ? $discountfilter = " AND IsDiscount = '1'" : $discountfilter = "";
        $filter = $thalsfilter.$premiumfilter.$discountfilter;
                
        $sqlQuery = new SQLQuery();
        $sqlQuery->select = array("Location.ID AS LocID", "Street");
        $sqlQuery->from = array("Shop", "LEFT JOIN Location ON Shop.LocationID = Location.ID");
        $sqlQuery->where = array($filter);
        $sqlQuery->orderby = "Street ASC";
        $sqlQuery->groupby = array("Location.ID");
        $result = $sqlQuery->execute();
        
        $results = array();
        $c = 0; 
        
        foreach($result as $row) {
            
             $results[] = array(
                'ID' => $row['LocID'], 
                'Name' => $row['Street']
             ); 
                       
        } 
        
        return new DataObjectSet($results); 
        
    }
    
    // Verkrijgen van de winkels gerangschikt op alfabet
    public function getShopsByAZ(){
        $shops = array();
        foreach(self::$shopLetters as $letter){
            $shops[] = array(
                'ShopLetter' => $letter,
                'Shops' => $this->ShopInfo($letter)  
            );
        }
   
        return new DataObjectSet($shops);
    }
    
    // Verkrijgen van de winkels gerangschikt op categorie
    public function getShopsByCat(){
        $shops = array();
        $categories = DataObject::get('Category','','Name ASC');
        
        if ($categories){ 
            foreach($categories as $category){
                $shops[] = array(
                    'ShopCategoryID' => $category->ID,
                    'ShopCategoryName' => $category->Name,
                    'Shops' => $this->ShopInfo(null,$category->ID)  
                );                
            }
        }
   
        return new DataObjectSet($shops);
    }
    
    // Verkrijgen van de winkels gerangschikt op straat
    public function getShopsByStreet(){
        $shops = array();
        $streets = DataObject::get('Location','','Street ASC');
        
        if ($streets){ 
            foreach($streets as $street){
                $shops[] = array(
                    'ShopStreetID' => $street->ID,
                    'ShopStreetName' => $street->Street,
                    'Shops' => $this->ShopInfo(null,null,$street->ID)  
                );                
            }
        }
   
        return new DataObjectSet($shops);
    }
    
    // Verkrijgen van de winkelinformatie
    public function ShopInfo($letter = null, $category = null, $street = null){
    
        $this->ThalsOnly ? $thalsfilter = " AND IsThals = '1'" : $thalsfilter = "";
        $this->PremiumOnly ? $premiumfilter = " AND IsPremium = '1'" : $premiumfilter = "";
        $this->DiscountOnly ? $discountfilter = " AND IsDiscount = '1'" : $discountfilter = "";
          
        if($letter) {
            if($letter == '0-9') {
                $query = DataObject::get("Shop", "FirstLetter IN ('1','2','3','4','5','6','7','8','9','0') ". $thalsfilter . $premiumfilter . $discountfilter, "Name ASC" );        
            } else {      
                $query = DataObject::get("Shop","FirstLetter = '".$letter."' ". $thalsfilter . $premiumfilter . $discountfilter, "Name ASC");   
            }
        
            if($query) {
                return $query;
            } else {
                return false;
            }
        
        } elseif($category) {
            $query = DataObject::get("Shop","Category.ID = '".$category."' ". $thalsfilter . $premiumfilter . $discountfilter, "Name ASC", "LEFT JOIN Shop_Categories ON Shop.ID = Shop_Categories.ShopID LEFT JOIN Category ON Shop_Categories.CategoryID = Category.ID "); 
            
            if($query) {
                return $query;
            } else {
                return false;
            }
                
        } elseif($street) {
            $query = DataObject::get("Shop","LocationID = '".$street."' ". $thalsfilter . $premiumfilter . $discountfilter, "Name ASC"); 
            
            if($query) {
                return $query;
            } else {
                return false;
            }
        
            
        } else {
            return false;
        }
         
    }
    
    public function getCurrentShop() {
        $Params = $this->getURLParams();
        $URLSegment = Convert::raw2sql($Params['ID']);
          
        if($URLSegment && $Shop = DataObject::get_one('Shop', "URLSegment = '" . $URLSegment . "'")) {      
            return $Shop;
        }
    }
     
    public function details() {
 
        if($Shop = $this->getCurrentShop()) {
            $Data = array(
                'Shop' => $Shop,
                'MetaTitle' => $Shop->Name
            );
              
            return $this->customise($Data)->renderWith(array('ShopPage_details', 'Page'));        
        }
        else {
            return $this->httpError(404, 'Sorry, deze winkel kon niet worden gevonden');
        }
    }
       

    
}
